package com.base.cn.platform.os.controller.mobile.qa.reply;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.service.course.qa.qareply.QaReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * H5问题评论Controller
 * @author LIZHIPENG
 * @create 2018-06-13 上午 10:05
 */
@Controller
@RequestMapping("/mobile")
public class MobileQaReplyController extends MobileBaseController {

    @Autowired
    private QaReplyService qaReplyService;

    /**
     * 查询问题评论列表
     * @param model Model
     * @param currentPage 页码
     * @param params 查询条件
     * @return String
     */
    @RequestMapping(value = {"/qa/queryQaReplyList", "/qa/queryChildQaReplyList"})
    public String queryQaReplyList(Model model, HttpServletRequest request,
                                         @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                         @RequestBody Map<String, Object> params){
        String _uri = request.getRequestURI();
        params.put("status",1);
        Map<String, Object> dataMap = qaReplyService.findQaReplyListPage(currentPage, 10, params, true, true);
        model.addAttribute("dataMap", dataMap);
        if(_uri.endsWith("queryQaReplyList")){
            return resultPage("/qa/reply/ajax-qa-reply-list");
        }else {
            return resultPage("/qa/reply/ajax-qa-allreply-list");
        }
    }

    /**
     * 保存评论
     * @param request HttpServletRequest
     * @param params 评论信息
     * @return Map<String, Object>
     */
    @RequestMapping("/uc/qa/reply/saveQaReply")
    @ResponseBody
    public Map<String, Object> saveQaReply(HttpServletRequest request,
                                           @RequestBody Map<String, Object> params){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("userId", userId);
        return qaReplyService.saveQaReply(params);
    }
}
